package com.example.dubboserver.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * 自定义Dubbo过滤器 - 实现服务调用统计
 */
@Activate(group = {CommonConstants.PROVIDER}, order = -9000)
public class DubboStatsFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String serviceName = invoker.getInterface().getSimpleName();
        String methodName = invocation.getMethodName();
        
        // 调用前统计
        System.out.printf("[Stats] 服务调用开始：%s.%s%n", serviceName, methodName);
        
        try {
            Result result = invoker.invoke(invocation);
            
            // 调用成功统计
            System.out.printf("[Stats] 服务调用成功：%s.%s%n", serviceName, methodName);
            
            return result;
        } catch (Exception e) {
            // 调用失败统计
            System.out.printf("[Stats] 服务调用失败：%s.%s，异常：%s%n", 
                    serviceName, methodName, e.getMessage());
            throw e;
        }
    }
}